package com.tnsw.facebookautoliker.base;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.tnsw.coreutils.MyLog;
import com.tnsw.coreutils.database.DBActionListener;
import com.tnsw.coreutils.ui.BitmapCache;
import com.tnsw.facebookautoliker.MainScreenActivity;
import com.tnsw.facebookautoliker.database.DBManager;
import com.tnsw.facebookautoliker.helper.FacebookActionListener;
import com.tnsw.facebookautoliker.helper.FacebookBGManager;
import com.tnsw.facebookautoliker.helper.FacebookCommandObject;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class FragmentBase extends Fragment {
	
	protected String tag = "FragmentBase";
	private FacebookCommandObject lastCommand;
	protected BitmapCache bmpCache;
	
	protected Integer[] visibleMenu = null;
	protected Menu currentMenu;
	
	
	public void setLastCommand(FacebookCommandObject lc) {
		this.lastCommand = lc;
	}
	
	protected void setVisibleMenu(Integer[] m) {
		this.visibleMenu = m;
		Arrays.sort(this.visibleMenu);
	}
	
	public Integer[] getVisibleMenu() {
		return(this.visibleMenu);
	}
	
	public void refreshOptionsMenu(Menu menu) {
		if (menu == null) {
			MyLog.d(tag, "currentMenu == null, exit now");
			return;
		}
		
		if (menu.size() == 0) {
			MyLog.d(tag, "menu.size()==0");
			return;
		}

		int i;		
		int size = menu.size();
		
		if (this.visibleMenu == null) {
			MyLog.d(tag, "visibleMenu == null, show all menu from Activity");
			for (i = 0; i < size; i++) {
				MenuItem item = menu.getItem(i);
				item.setVisible(true);
			}
			return;
		}
		
		for (i = 0; i < size; i++) {
			MenuItem item = menu.getItem(i);
			int idx = Arrays.binarySearch(this.visibleMenu, item.getItemId());
			if (idx >= 0) {
				item.setVisible(true);
				MyLog.d(tag, "visible menu for " + item.getTitle() + ", " + item.getItemId());

			} else {
				item.setVisible(false);
				MyLog.d(tag, "invisible menu for " + item.getTitle() + ", " + item.getItemId());
			}
		}
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		MyLog.d(tag, "onCreateOptionsMenu() - Fragment");
		this.currentMenu = menu;
		
		this.refreshOptionsMenu(menu);
	}
	
	public void setBitmapCache(BitmapCache bmpCache) {
		MyLog.d(tag, "setBitmapCache() is called");
		if (bmpCache == null) {
			MyLog.e(tag, "but bmpCache is null!!");
		}
		this.bmpCache = bmpCache;
	}
	
	public BitmapCache getBitmapCache() {
		return(this.bmpCache);
	}
	
	@Override
	public void onResume() {
		MyLog.d(tag, "onResume()");
		super.onResume();
	}
	
	protected FacebookCommandObject getLastCommand() {
		return(this.lastCommand);
	}
	
	public void onFacebookCommandExecute(FacebookCommandObject fbco) {
		
	}
	
	public void onTabReselected() {
		MyLog.d(tag, "onTabReselected - " + this.getClass().getName());
	}
	
	public void onTabSelected() {
		MyLog.d(tag, "onTabSelected - " + this.getClass().getName());
	}
	
	public void onTabUnselected() {
		MyLog.d(tag, "onTabUnselected - " + this.getClass().getName());
	}
	
	public void executeLastCommand() {
		if (this.getActivity() instanceof MainScreenActivity) {
			MainScreenActivity activity = (MainScreenActivity) this.getActivity();
			if (activity.getLatestResultCode() == 9999) {
				MyLog.d(tag, "executeLastCommand() is aborted because latestResultCode=9999, suspected that this is returned from pressing 'back' from ConnectionProblemActivity");
				return;
			}
		}
		
		if (this.lastCommand != null) {
			this.lastCommand.execute();
		} else {
			MyLog.d(tag, "Not last command for fragment:" + this.getClass().getName());
		}
	}
	
	@Override
	public void onStop() {
		super.onStop();
		//MyLog.d(tag, "onStop()");
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		//MyLog.d(tag, "onDestroy()");
	}
	
	@Override
	public void onDetach() {
		super.onDetach();
		
		FacebookBGManager fbm = FacebookBGManager.getInstance(null);
		if (fbm != null) {
			if (this instanceof FacebookActionListener) {
				fbm.unregisterActionListener((FacebookActionListener) this);	
			}
				
		}
		
		DBManager dbm = DBManager.getInstance(null);
		if (dbm != null) {
			if (this instanceof DBActionListener) {
				dbm.unregisterActionListener((DBActionListener) this);
			}
		}
	
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		MyLog.d(tag, "onActivityCreated()");
	}
	
	@Override
	public void onSaveInstanceState (Bundle outState) {
		super.onSaveInstanceState(outState);
		MyLog.d(tag, "onSaveInstanceState()");
	}
	
}
